package com.playball.lottery.util;

import java.util.ArrayList;
import java.util.Calendar;

import com.playball.lottery.R;
import com.playball.lottery.widget.DateUtils;
import com.playball.lottery.widget.TosGallery;
import com.playball.lottery.widget.WheelView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * 
* @ClassName: LuckySelBirthDialog 
* @Description: TODO(幸运选好里面的 生日的弹窗口) 
* @author vanszhen 
* @date 2014年5月9日 上午9:35:13 
*
 */
public class LuckyWheelBirthDialog {
	
	
	 private Context context;
	 /** LayoutInflater */ 
	private LayoutInflater inflater;

	private View view;
	private Dialog dialog;

	private ArrayList<TextInfo> mMonths = new ArrayList<TextInfo>();
	private ArrayList<TextInfo> mYears = new ArrayList<TextInfo>();
	private ArrayList<TextInfo> mDates = new ArrayList<TextInfo>();

	private WheelView mDateWheel = null;
	private WheelView mMonthWheel = null;
	private WheelView mYearWheel = null;

	private int mCurDate = 0;
	private int mCurMonth = 0;
	private int mCurYear = 0;

	public LuckyWheelBirthDialog(Context context) {
		super();
		this.context = context;
		inflater = LayoutInflater.from(context);
	}
	
	 /**初始化布局*/ 
	private void initView() {
		mDateWheel = (WheelView) view.findViewById(R.id.wheel_date);
		mMonthWheel = (WheelView) view.findViewById(R.id.wheel_month);
		mYearWheel = (WheelView) view.findViewById(R.id.wheel_year);

		mDateWheel.setOnEndFlingListener(mListener);
		mMonthWheel.setOnEndFlingListener(mListener);
		mYearWheel.setOnEndFlingListener(mListener);

		mDateWheel.setSoundEffectsEnabled(true);
		mMonthWheel.setSoundEffectsEnabled(true);
		mYearWheel.setSoundEffectsEnabled(true);

		mDateWheel.setAdapter(new WheelTextAdapter(context));
		mMonthWheel.setAdapter(new WheelTextAdapter(context));
		mYearWheel.setAdapter(new WheelTextAdapter(context));

		prepareData();

		
		
		Button btnOk=(Button) view.findViewById(R.id.btnwheelsure);
		btnOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if( listener != null ){
					listener.onDateGet(getFormatDate());
					destoryDialog();
				}
					
			}
		});
		
		Button btnNo=(Button) view.findViewById(R.id.btnwheelcance);
		btnNo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				destoryDialog();
			}
		});

	}

	 /**接口*/ 
	public interface onClickListener{
		void onDateGet(String date);
	};
	
	onClickListener listener;
	
	public void setListener(onClickListener l){
		this.listener=l;
	}
	
	
    public  void initDialogDate() {
    	view = inflater.inflate(R.layout.jdd_lucky_selbirth, null);
    	LinearLayout layout = (LinearLayout) view.findViewById(R.id.jdd_lucky_sel_birth);
    	initView();
    	dialog = new Dialog(context, R.style.FullHeightDialog);
		dialog.setCancelable(true);
	      
		
		
       /* 将对话框的大小按屏幕大小的百分比设置*/
		Window dialogWindow=dialog.getWindow();
		DisplayMetrics metrics=new DisplayMetrics();
		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int width=(int) ((metrics.widthPixels)*0.8);
		//int height=metrics.heightPixels;
		WindowManager.LayoutParams layoutParams=dialog.getWindow().getAttributes();
		layoutParams.width=width;
		dialogWindow.setAttributes(layoutParams);
		dialog.show();
		dialog.setContentView(layout, new LinearLayout.LayoutParams(width,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失   
    
	}

 
	public void destoryDialog(){
		dialog.dismiss();
	}
    
	private TosGallery.OnEndFlingListener mListener = new TosGallery.OnEndFlingListener() {
		@Override
		public void onEndFling(TosGallery v) {
			int pos = v.getSelectedItemPosition();

			if (v == mDateWheel) {
				TextInfo info = mDates.get(pos);
				setDate(info.mIndex);
			} else if (v == mMonthWheel) {
				TextInfo info = mMonths.get(pos);
				setMonth(info.mIndex);
			} else if (v == mYearWheel) {
				TextInfo info = mYears.get(pos);
				setYear(info.mIndex);
			}

		}
	};

	 /**
	 * 得到日期
	* @Title: formatDate 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */ 
	@SuppressLint("DefaultLocale")
	public String getFormatDate() {
		return String.format("%d-%02d-%02d", mCurYear, mCurMonth + 1,
				mCurDate);
	}

	private void setDate(int date) {
		if (date != mCurDate) {
			mCurDate = date;
		}
	}

	private void setYear(int year) {
		if (year != mCurYear) {
			mCurYear = year;
		}
	}

	private void setMonth(int month) {
		if (month != mCurMonth) {
			mCurMonth = month;

			Calendar calendar = Calendar.getInstance();
			int date = calendar.get(Calendar.DATE);
			prepareDayData(mCurYear, month, date);
		}
	}

	private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31,
			31, 30, 31, 30, 31 };

	private static final String[] MONTH_NAME = { "一月", "二月",
			"三月", "四月", "五月", "六月", "七月", "八月", "九月",
			"十月", "十一月", "十二月", };

	private boolean isLeapYear(int year) {
		return ((0 == year % 4) && (0 != year % 100) || (0 == year % 400));
	}

	private void prepareData() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		int startYear = 1960;
		int endYear = 2060;

		mCurDate = day;
		mCurMonth = month;
		mCurYear = year;

		for (int i = 0; i < MONTH_NAME.length; ++i) {
			mMonths.add(new TextInfo(i, MONTH_NAME[i], (i == month)));
		}

		for (int i = startYear; i <= endYear; ++i) {
			mYears.add(new TextInfo(i, String.valueOf(i), (i == year)));
		}

		((WheelTextAdapter) mMonthWheel.getAdapter()).setData(mMonths);
		((WheelTextAdapter) mYearWheel.getAdapter()).setData(mYears);

		prepareDayData(year, month, day);

		mMonthWheel.setSelection(month);
		mYearWheel.setSelection(year - startYear);
		mDateWheel.setSelection(day - 1);
	}

	private void prepareDayData(int year, int month, int curDate) {
		mDates.clear();

		int days = DAYS_PER_MONTH[month];

		// The February.
		if (1 == month) {
			days = isLeapYear(year) ? 29 : 28;
		}

		for (int i = 1; i <= days; ++i) {
			mDates.add(new TextInfo(i, String.valueOf(i), (i == curDate)));
		}

		((WheelTextAdapter) mDateWheel.getAdapter()).setData(mDates);
	}

	protected class TextInfo {
		public TextInfo(int index, String text, boolean isSelected) {
			mIndex = index;
			mText = text;
			mIsSelected = isSelected;

			if (isSelected) {
				mColor = Color.BLUE;  
			}
		}

		public int mIndex;
		public String mText;
		public boolean mIsSelected = false;
		public int mColor = Color.BLACK;
	}

	protected class WheelTextAdapter extends BaseAdapter {
		ArrayList<TextInfo> mData = null;
		int mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
		int mHeight = 50;
		Context mContext = null;

		public WheelTextAdapter(Context context) {
			mContext = context;
			mHeight = (int) DateUtils.pixelToDp(context, mHeight);
		}

		public void setData(ArrayList<TextInfo> data) {
			mData = data;
			this.notifyDataSetChanged();
		}

		public void setItemSize(int width, int height) {
			mWidth = width;
			mHeight = (int) DateUtils.pixelToDp(mContext, height);
		}

		@Override
		public int getCount() {
			return (null != mData) ? mData.size() : 0;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView textView = null;

			if (null == convertView) {
				convertView = new TextView(mContext);
				convertView.setLayoutParams(new TosGallery.LayoutParams(mWidth,
						mHeight));
				textView = (TextView) convertView;
				textView.setGravity(Gravity.CENTER);
				textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
				textView.setTextColor(Color.BLACK);
			}

			if (null == textView) {
				textView = (TextView) convertView;
			}

			TextInfo info = mData.get(position);
			textView.setText(info.mText);
			textView.setTextColor(info.mColor);

			return convertView;
		}
	}
}
